<template>
  <div class="app-container">
    <div class="custom_card_0" v-show="showSearch">
      <el-form
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        label-width="68px"
      >
        <el-form-item label="Code" prop="code">
          <el-input
            v-model="queryParams.code"
            placeholder="请输入code"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="业务分类" prop="bizClassification">
          <el-select
            v-model="queryParams.bizClassification"
            clearable
            @clear="tenantBusiness"
            style="width: 220px"
          >
            <template v-for="dict in tenantBusiness">
              <el-option
                :label="dict.busName"
                :value="dict.busId"
                :key="dict.busId"
              ></el-option>
            </template>
          </el-select>
        </el-form-item>
        <el-form-item label="模板名称" prop="contractTemplateName">
          <el-input
            v-model="queryParams.contractTemplateName"
            placeholder="请输入模板名称"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="handleQuery"
            >搜索</el-button
          >
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
            >重置</el-button
          >
        </el-form-item>
      </el-form>
    </div>
    <div class="custom_card">
      <el-row :gutter="10" class="mb10">
        <el-col :span="1.5">
          <el-button
            class="first-plain-btn"
            size="medium"
            icon="el-icon-plus"
            @click="handleAdd"
            v-hasPermi="['contract:template:add']"
            >新增</el-button
          >
        </el-col>
        <right-toolbar
          :showSearch.sync="showSearch"
          @queryTable="getList"
        ></right-toolbar>
      </el-row>

      <el-table v-loading="loading" :data="templateList" border>
        <el-table-column
          label="Code"
          align="center"
          prop="code"
          min-width="120"
          show-overflow-tooltip
        />
        <el-table-column
          label="业务分类"
          align="center"
          prop="bizClassification"
          min-width="120"
          show-overflow-tooltip
        >
          <template slot-scope="scope">
            <dict-tag
              :options="dict.type.sys_business"
              :value="scope.row.bizClassification"
            />
          </template>
        </el-table-column>
        <el-table-column
          label="合同模板地址"
          align="center"
          prop="contractTemplateAddress"
          min-width="240"
          show-overflow-tooltip
        />
        <el-table-column
          label="合同模板名称"
          align="center"
          prop="contractTemplateName"
          min-width="120"
          show-overflow-tooltip
        />
        <el-table-column
          label="所属租户"
          align="center"
          prop="tenantName"
          min-width="240"
          show-overflow-tooltip
        />
        <el-table-column
          label="操作"
          align="center"
          class-name="small-padding fixed-width"
          min-width="120"
          fixed="left"
        >
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-show"
              @click="handleShow(scope.row)"
              >查看</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>

    <!-- 添加或修改合同模板对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="业务分类" prop="bizClassification">
          <el-select
            v-model="form.bizClassification"
            clearable
            @clear="tenantBusiness"
            style="width: 100%"
          >
            <template v-for="dict in tenantBusiness">
              <el-option
                :label="dict.busName"
                :value="dict.busId"
                :key="dict.busId"
              ></el-option>
            </template>
          </el-select>
        </el-form-item>
        <el-form-item label="模板地址" prop="contractTemplateAddress">
          <el-input
            v-model="form.contractTemplateAddress"
            placeholder="请输入合同模板地址"
          />
        </el-form-item>
        <el-form-item label="模板名称" prop="contractTemplateName">
          <el-input
            v-model="form.contractTemplateName"
            placeholder="请输入合同模板名称"
          />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input
            v-model="form.remark"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog
      :visible.sync="imgVisble"
      width="700px"
      class="imgVisble"
      title="合同模板详情"
      :close-on-click-modal="false"
    >
      <iframe
        :src="imgVisbleUrl"
        frameborder="0"
        width="100%"
        height="600"
      ></iframe>
    </el-dialog>
  </div>
</template>

<script>
import { listTemplate, getTemplate, addTemplate } from '@/api/contract/template'
import { tenantBusiness } from '@/api/car/business'

export default {
  name: 'Template',
  dicts: ['sys_business'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 合同模板表格数据
      templateList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      tenantBusiness: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        code: null,
        bizClassification: null,
        contractTemplateName: null,
      },
      imgVisble: false,
      imgVisbleUrl: null,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        bizClassification: [
          {
            required: true,
            message: '请选择业务类型',
            trigger: 'change',
          },
        ],
        contractTemplateAddress: [
          {
            required: true,
            message: '请输入模板地址',
            trigger: 'blur',
          },
        ],
        contractTemplateName: [
          {
            required: true,
            message: '请输入合同模板名称',
            trigger: 'blur',
          },
        ],
      },
    }
  },
  created() {
    this.getList()
    this.tenantBusinessList()
  },
  mounted() {
    this.$IVue.$on('fevent', (val) => {
      this.getList()
    })
  },
  methods: {
    tenantBusinessList() {
      tenantBusiness().then((response) => {
        this.tenantBusiness = response.data
      })
    },
    /** 查询合同模板列表 */
    getList() {
      this.loading = true
      listTemplate(this.queryParams).then((response) => {
        this.templateList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        code: null,
        bizClassification: null,
        contractTemplateName: null,
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加合同模板'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$router.push({ path: '/contract/editContract/' + row.contractId })
    },
    handleShow(row) {
      this.imgVisbleUrl =
        'https://view.officeapps.live.com/op/view.aspx?src=' +
        this.getImgUrl(row.contractTemplateAddress)
      this.imgVisble = true
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (this.form.contractId != null) {
            updateTemplate(this.form).then((response) => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addTemplate(this.form).then((response) => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
  },
}
</script>
